GPIO Control

After installing the Vivado and Vitis environments on Ubuntu, you can develop applications using Vitis on the Ubuntu system. Similar to Windows, you need to first create a platform project and then create the corresponding application project.

Setting Up Vitis Project

Creating Platform Project

image-20250709112858375

When we created the Petalinux project, we already imported the XSA file into Ubuntu. At this point, select this XSA file and choose Linux as the system: click finish.

image-20250709112955359

Creating Application Project

Create an application project. This time we will only create one application project and organize different peripheral tests into modules. First, create an application project:

image-20250709113105988

image-20250709113135071

image-20250709113153767

image-20250710100458596

image-20250710100546445

image-20250709113326894

Click finish, and the application project is created.

The current GPIO configuration enables 2 MIO LEDs/2 EMIO LEDs, 2 MIO KEYs/2 EMIO KEYs, and one buzzer.

Controlling GPIO via sysfs

In the Linux system, GPIO can be conveniently controlled through the sysfs interface. Below is how to use sysfs to control GPIO on the Zynq-7020 platform.

Confirming GPIO Number

Zynq-7020 GPIO is divided into:

First, you need to determine the number of the GPIO you want to control in the Linux system. The gpiochips number under /sys/class/gpio corresponds to the first GPIO and MIO0 number, which is generally 906.

image-20250709114610103

GPIO number calculation formula:

For example:

Exporting GPIO

Assuming you want to control EMIO0, you need to export this GPIO first.

Setting GPIO Direction

Set GPIO to output or input mode:

Reading and Writing GPIO Values

Output mode (controlling LEDs, etc.):

Input mode (reading buttons, etc.):

Unexporting GPIO

After use, you can unexport:

Controlling via Program

Adding Code to Vitis Project

Right-click "src"->"new"->"Folder" to create GPIO and TEST folders:

image-20250710120437525

image-20250710120520283

Right-click "GPIO"->"new"->"File" to create corresponding .c/.h files; right-click "TEST"->"new"->"File" to create corresponding .c files; right-click "src"->"new"->"File" to create main.c/main.h.

image-20250710120556640

After adding the corresponding code, the final project structure is as follows:

image-20250710120833354

Compilation and Debugging

Connecting IP Address

Open Vitis, click the following icon:

image-20250710141903115

Select Linux Agent as default, and double-click to configure this Agent:

image-20250710142023761

In the pop-up interface, enter the development board's IP address in the Host field, keep the port as default, no need to change:

image-20250710142109417

After filling in, you can click Test Connection to test whether the connection is successful or failed. The following content indicates a successful connection:

image-20250710142239180

Click ok to exit.

Modifying Program Download Path

Because the default download path is the first partition of the SD card, and the default first partition is used to store kernel images, BOOT.BIN, and other information. So create a new folder app under /home/root and download the compiled .elf file to this folder.

image-20250714121606681

image-20250714121125768

Debugging

You can directly click the icon in the upper left corner to compile the code, or click "build project" as before to compile:

image-20250710133350018

If this type of error occurs

image-20250710135004129

The corresponding solution is to add the header file path to the search path.

Right-click the project, select "C/C++ Build Settings":

image-20250710134241403

image-20250710134400330

image-20250710135133836

After "Add":

image-20250710135148364

After adding, the page:

image-20250710135654166

Now it can compile successfully.

During Debug, Vitis will download the code to the development board's /home/root/app directory to run. At this point, you can use Vitis for single-step debugging or breakpoint debugging.

Core Code